package com.bw.mallshop.bind;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bw.mallshop.R;
import com.bw.mallshop.adapter.ConfirmOrderGoodsAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;

public class OrderBind {

    @BindingAdapter({"orderAdapter","shipname","shipaddress","shipflag","shipclick"})
    public static void orderBind(RecyclerView recyclerView,
                                 BaseQuickAdapter orderAdapter,
                                 String shipname, String shipaddress,
                                 boolean shipflag, View.OnClickListener shipClick){
        RecyclerView.Adapter recyclerAdapter = recyclerView.getAdapter();
        if (recyclerAdapter==null) {
            LinearLayoutManager manager = new LinearLayoutManager(recyclerView.getContext());
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(manager);
            View headView = LayoutInflater.from(recyclerView.getContext())
                    .inflate(R.layout.view_confirm_head, null);
            View footView = LayoutInflater.from(recyclerView.getContext())
                    .inflate(R.layout.view_confirm_foot, null);
            TextView mSelectShipTv = headView.findViewById(R.id.mSelectShipTv);
            RelativeLayout mShipView = headView.findViewById(R.id.mShipView);
            TextView mShipNameTv = headView.findViewById(R.id.mShipNameTv);
            TextView mShipAddressTv = headView.findViewById(R.id.mShipAddressTv);
            mSelectShipTv.setOnClickListener(shipClick);
            mShipView.setOnClickListener(shipClick);
            if (shipflag) {
                mShipView.setVisibility(View.VISIBLE);
                mSelectShipTv.setVisibility(View.GONE);
                mShipNameTv.setText(shipname);
                mShipAddressTv.setText(shipaddress);
            } else {
                mShipView.setVisibility(View.GONE);
                mSelectShipTv.setVisibility(View.VISIBLE);
            }
            orderAdapter.addHeaderView(headView);
            orderAdapter.addFooterView(footView);
            recyclerView.setAdapter(orderAdapter);
        }else {
            ConfirmOrderGoodsAdapter confirmOrderGoodsAdapter = (ConfirmOrderGoodsAdapter) recyclerAdapter;
            View headView = confirmOrderGoodsAdapter.getHeaderLayout().getChildAt(0);
            TextView mSelectShipTv = headView.findViewById(R.id.mSelectShipTv);
            RelativeLayout mShipView = headView.findViewById(R.id.mShipView);
            TextView mShipNameTv = headView.findViewById(R.id.mShipNameTv);
            TextView mShipAddressTv = headView.findViewById(R.id.mShipAddressTv);
            mSelectShipTv.setOnClickListener(shipClick);
            mShipView.setOnClickListener(shipClick);
            LogUtils.e("orderBind"+shipname+"&&"+shipaddress);
            mShipView.setVisibility(View.VISIBLE);
            mSelectShipTv.setVisibility(View.GONE);
            mShipNameTv.setText(shipname);
            mShipAddressTv.setText(shipaddress);
        }
    }

}
